<template>
  <div class="view-table-wrap">
    <div class="view-table-content" v-for="(rowData, index) in columns" :key="index">
      <div class="view-table-item" :style="`flex-basis: ${getColBasis(colData.col, rowData)}`" v-for="colData in rowData" :key="colData.value">
        <div class="label">{{ colData.label }}</div>
        <div class="text">
          <slot name="value" :scope="{ col: tableData[colData.value], index, prop: colData.value, data: tableData }">
            {{ tableData[colData.value] || "-" }}
          </slot>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from "vue";
type TableItem = Partial<{
  label: string;
  value: string;
  col?: number;
  customFun?: Function;
}>;
const props = defineProps<{
  columns?: TableItem[][];
  data: any;
}>();
const tableData = computed(() => props.data || {});
const columns = computed(() => props.columns);
const getColBasis = (col = 1, rowData) => {
  if (col === "max") {
    return "100%";
  }
  const len = rowData
    .map(item => item.col || 1)
    .reduce((pre, cur) => {
      return pre + cur;
    });
  const basis = `${(100 / len) * col}%`;
  return basis;
};
</script>
<style lang="scss" scoped>
.view-table-wrap {
  width: 100%;
  margin-bottom: 16px;
  border-top: 1px solid #dfdfdf;
  border-left: 1px solid #dfdfdf;

  .view-table-content {
    display: flex;
    flex-wrap: wrap;
    color: #000;

    .view-table-item {
      display: flex;
      flex-grow: 1;
      min-height: 40px;
      line-height: 22px;
      border-right: 1px solid #dfdfdf;
      border-bottom: 1px solid #dfdfdf;

      .label {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        width: 120px;
        padding: 8px 12px;
        font-size: 14px;
        font-weight: bold;
        background: #e7eefb;
        border-right: 1px solid #dfdfdf;
      }

      .text {
        display: flex;
        flex: 1;
        align-items: center;
        flex-wrap: wrap;
        padding: 0 12px;
        padding-top: 8px;
        padding-bottom: 8px;
        font-size: 14px;
        line-height: 18px;
        word-break: break-all;

        .tag {
          height: 22px;
          padding: 0 8px;
          font-size: 12px;
          line-height: 22px;
          color: rgb(0 0 0 / 80%);
          text-align: center;
          border-radius: 4px;
        }
      }

      &.large {
        width: 66.6666%;
      }

      &.plus {
        width: 100%;
      }
    }
  }

  &.view-table-wrap-second {
    margin-bottom: 12px;
    border-top: 0;

    .view-table-content {
      justify-content: space-between;

      .view-table-item {
        border-top: 1px solid #dfdfdf;
      }

      .view-table-btn {
        display: flex;
        align-items: center;
        height: 40px;
        margin-left: 16px;
        line-height: 40px;
      }
    }
  }
}
</style>
